package com.wiscamp.ninechapters.problems.domain.models.problems;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.io.FileInputStream;

public class ImageHelper {
    private ImageHelper() {
    }

    private static final Logger logger = LoggerFactory.getLogger(ImageHelper.class);

    /**
     * 获取图片文件的高度宽度
     *
     * @param fileName 本地文件名（带有路径）
     * @return
     */
    public static ImageSize getImageSize(String fileName) {
        try {
            var inputStream = new FileInputStream(fileName);
            var bufferedImage = ImageIO.read(inputStream);
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            inputStream.close();
            return new ImageSize(height, width);
        } catch (Exception ex) {
            logger.error("Failed to get the size of image.", ex);
            return new ImageSize(0, 0);
        }
    }
}
